#include <string.h>
#include "mysql.h"
#include "zlog.h"

#define ERR_SUCCESS 0
#define ERR_FAILED  1
#define HOST "192.168.10.153"
#define USER "root"
#define PASS "123456"
#define DB "runoob"
#define PORT 6520
#define SQL_MAX_LEN     1024

int init_log ()
{
    int ret = dzlog_init("test.conf", "test_cat");
    if (ERR_SUCCESS != ret) 
    {
        printf("init zlog failed, ret:%d", ret);
        return ERR_FAILED;
    }

    return ERR_SUCCESS;
}
int batch_insert (MYSQL *mysqlConn, char* sql, int rows)
{
    int i = 0; 
    for (i = 0; i < rows; i++)
    {
        //dzlog_debug("insert row:%d ", i);
        int ret = mysql_real_query (mysqlConn, sql, strlen(sql));
        if (ERR_SUCCESS != ret)
        {
            ret = ERR_FAILED;
            dzlog_error ("mysql_real_query failed. SQL:%s. %s", sql, mysql_error(mysqlConn));

            return ERR_FAILED;
        }
    }

    return ERR_SUCCESS;
}

int main(int argc, char *argv[])
{
    (void) init_log ();

    MYSQL *mysqlConn = NULL;
    mysqlConn = mysql_init(NULL);
    if (NULL == mysqlConn)
    {
        dzlog_error("mysql_init failed");
        return -1;
    }

    char *host = argv[1];
    if (NULL == mysql_real_connect(mysqlConn, host, USER, PASS, DB, PORT, NULL, 0))
    {
        dzlog_error("mysql_real_connect failed, %s", mysql_error(mysqlConn));
        return -1;
    }

    int rows = 0;
    if (NULL == argv[2])
    {
        rows = 1000;
    }
    else
    {
        rows = atoi (argv[2]);
        if (0 == rows)
        {
            rows = 1000;
        }
    }

    printf ("insert rows %d\n", rows);
    batch_insert(mysqlConn, "insert into text values (NULL, '郭涧川', 'HangZhouYuhang', 'sss', 'sss')", rows);

    zlog_fini();
    mysql_close (mysqlConn);

    return 0;
}
